// 白名单，这里面的属性不会被 clear 方法清空
const whitelist = [];

const db = {
  save(key, value) {
    uni.setStorageSync(key, JSON.stringify(value));
  },
  get(key, defaultValue = {}) {
    if (uni.getStorageSync(key)) {
      return JSON.parse(uni.getStorageSync(key));
    } else {
      return defaultValue;
    }
  },
  remove(key) {
    uni.removeStorageSync(key);
  },
  clear() {
    let whiteMap = new Map();
    whitelist.forEach((item) => {
      const element = this.get(item);
      if (element) {
        whiteMap.set(item, element);
      }
    });
    uni.clearStorageSync();
    for (let i = 0; i < whitelist.length; i++) {
      if (whiteMap.has(whitelist[i])) {
        this.save(whitelist[i], whiteMap.get(whitelist[i]));
      }
    }
  },
};

export default db;
